package com.aplink.generic.xmlparser;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.aplink.generic.pojo.GenericObject;

import java.util.ArrayList;

public class CustomListviewBehavior {
    private String desiredActivity;
    private boolean mDeleteItem;
    private boolean mItemClickListener;
    private boolean mItemLongClickListener;

    private boolean moveTo;
    private boolean mSwipeDetector;
    private ArrayList<String> passValueList;

    public CustomListviewBehavior() {

        this.desiredActivity = "";
        this.passValueList = new ArrayList<String>();
    }

    public boolean getDeleteItem() {
        return this.mDeleteItem;
    }

    public String getDesiredActivity() {
        return this.desiredActivity;
    }

    public boolean getItemClickListener() {
        return this.mItemClickListener;
    }

    public boolean getItemLongClickListener() {
        return this.mItemLongClickListener;
    }

    public boolean getMoveTo() {
        return this.moveTo;
    }

    public ArrayList<String> getPassValueList() {
        return this.passValueList;
    }

    public boolean getSwipDetector() {
        return this.mSwipeDetector;
    }

    public ListView setBehaviorForm( final Context mContext, ListView lv ) {

        if ( this.mItemClickListener == true ) {
            lv.setOnItemClickListener( new OnItemClickListener() {
                @Override
                public void onItemClick( AdapterView<?> arg0, View arg1,
                                         int position, long id ) {
                    final GenericObject genericObject = (GenericObject) arg1.getTag();
                    // parse which activity you wanna go
                    Toast.makeText(
                            mContext.getApplicationContext(),
                            "Outlet ID is"
                                    + String.valueOf( genericObject
                                    .getAttributeList().get( 0 )
                                    .getValue() ), Toast.LENGTH_LONG )
                            .show();

                    final String desiredActivity = CustomListviewBehavior.this.getDesiredActivity();
                    Class<?> c = null;
                    if ( desiredActivity != null ) {
                        try {
                            final String fullClassName = mContext
                                    .getApplicationContext().getPackageName()
                                    + "." + desiredActivity;
                            c = Class.forName( fullClassName );
                        } catch ( final ClassNotFoundException e ) {
                            e.printStackTrace();
                            return;
                        }
                    }

                    final Intent detailIntent = new Intent( mContext, c );
                    // we should replace this line based on parameters gained
                    // from above part
                    detailIntent.putExtra( "GenericObject", genericObject );
                    ( (FragmentActivity) mContext ).startActivityForResult(
                            detailIntent, 1 );
                }
            } );
        }

        return lv;
    }

    public void setDeleteItem( boolean deleteItem ) {
        this.mDeleteItem = deleteItem;
    }

    public void setDesiredActivity( String desiredactivity ) {
        this.desiredActivity = desiredactivity;
    }

    public void setItemClickListener( boolean _onItemClickListener ) {
        this.mItemClickListener = _onItemClickListener;
    }

    public void setItemLongClickListener( boolean mItemLongClickListener ) {
        this.mItemLongClickListener = mItemLongClickListener;
    }

    public void setMoveTo( boolean moveto ) {
        this.moveTo = moveto;
    }

    public void setPassValueList( ArrayList<String> passvalueList ) {
        this.passValueList = passvalueList;
    }

    public void setSwipDetector( boolean swipDetector ) {
        this.mSwipeDetector = swipDetector;
    }
}
